<template>
  <div class="login">
    <div class="loginBox">
      <div class="avtor">
        <img
          src="https://ftp.bmp.ovh/imgs/2021/06/aa9fd5cbe5f8f1a2.png"
          alt=""
        />
      </div>

      <el-form
        label-width="90px"
        class="loginForm"
        ref="loginform"
        :model="formdata"
        :rules="rules"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="formdata.username"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="iconfont icon-password"
            v-model="formdata.password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item class="buttons">
          <el-button @click="login" type="primary">确定</el-button>
          <el-button @click="reset" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  props: [],
  components: {},
  data() {
    return {
      formdata: {
        username: "",
        password: "",
 
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 12,
            message: "请输入3到12个字符用户名",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 12, message: "请输入6到12个密码", trigger: "blur" },
        ],
      },
    };
    
  },
  //方法 函数写这里
  methods: {
      reset(){
          console.log('reset')
          this.$refs.loginform.resetFields()
      },
      login() {
          console.log('login') 
          this.$refs.loginform.validate(async (valid)=>{
              console.log('valid',valid)
              if(!valid) {
                  this.$message('输入用户密码不规范')
                  return 
              }

            // 下面就开始请求数据库了 请求服务端
            const res = await this.$http.get('/api/login',{
                params:this.formdata
            })
            console.log('res',res)
            
            if(res.data.code !=1 ) {
                this.$message('用户名密码输入不正确请重试')
                return 
            }

            this.$message.success('登录成功准备跳转')
            window.sessionStorage.setItem('token',res.data.token)

            this.$router.push('/home')




          })
      }
  },
  //计算属性
  computed: {},
  //侦听器
  watch: {},
  //过滤器
  filters: {},
  //以下是生命周期
  //组件创建之前
  beforeCreate() {},
  //组件创建之后
  created() {},
  //页面渲染之前
  beforeMount() {},
  //页面渲染之后
  mounted() {},
  //页面视图数据更新之前
  beforeUpdate() {},
  //页面视图数据更新之后
  updated() {},
  //页面销毁之前
  beforeDestroy() {},
  //页面销毁之后
  destroyed() {},
};
</script>


<style lang="scss"  scoped>
.login {
  background-color: darkcyan;
  height: 100vh;
}

.loginBox {
  width: 450px;
  height: 350px;
  background-color: #ffffff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  border-radius: 5px;
}

.avtor {
  height: 60px;
  width: 60px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 5px;
  box-shadow: 0 0 12px grey;
  position: absolute;
  transform: translate(-15%, -15%);
  background-color: #ffffff;
}

.avtor img {
  height: 60px;
  width: 60px;
  border-radius: 50%;
  background-color: #eeee;
}

.loginForm {
  position: absolute;
  bottom: 30px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;
}

.buttons {
  display: flex;
}
</style>